package com.xiaozhi.config;

import com.xiaozhi.bean.Cat;
import com.xiaozhi.bean.Person;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.*;
import org.springframework.core.type.AnnotationMetadata;

@Configuration
// @Import(Person.class)
//如果要导入两个用下面的方式
@Import({Person.class, MyConfig.MyImportBeanDefinitionRegistrar.class})
@ComponentScan(value = "com.xiaozhi") //这里一定要进行包扫描
public class MyConfig {

	public MyConfig(){
		System.out.println("配置类无参构造方法执行了~");
	}

//	@Bean
//	public Person person(){
//		Person person=new Person();
//		person.setName("XIAOYAN");
//		return person;
//	}

	/**
	 * BeanDefinitionRegistry:Bean定义信息注册中心,图纸中心
	 * 它里面都是BeanDefinition
	 */
	static class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
		@Override
		public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
											BeanDefinitionRegistry registry) {

			//BeanDefinition
			RootBeanDefinition catDefinition = new RootBeanDefinition();
			catDefinition.setBeanClass(Cat.class);
			//catDefinition.setInstanceSupplier(()->new Cat());
			//Spring 这个实例的类型、名字
			registry.registerBeanDefinition("tomcat",catDefinition);
		}
	}
}
